package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.domin.Banner;
import org.csu.mypetstore.persistence.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl {
    @Autowired
    BannerMapper bannerMapper;

    public Banner getAllBanners(String username){
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return bannerMapper.selectOne(queryWrapper);
    }

    public void addBanner(Banner banner){
        bannerMapper.insert(banner);
    }
    public void deleteBanner(String username){
        bannerMapper.delete(new QueryWrapper<Banner>().eq("username",username));
    }
    public void addCount(String username,String column){
        UpdateWrapper<Banner> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username",username);
        String sql = String.format("%s=%s+1",column,column);
        updateWrapper.setSql(sql);
        bannerMapper.update(null,updateWrapper);
    }
}
